// Add semicolon to prevent IIFE from being passed as argument to concatenated code.
;
// UMD (Universal Module Definition)
// see https://github.com/umdjs/umd/blob/master/returnExports.js
(function (root, factory) {
    /* global define, exports, module */
    if (typeof define === 'function' && define.amd) {
        // AMD. Register as an anonymous module.
        define(factory);
    } else if (typeof exports === 'object') {
        // Node. Does not work with strict CommonJS, but
        // only CommonJS-like enviroments that support module.exports,
        // like Node.
        module.exports = factory();
    } else {
        // Browser globals (root is window)
        root.TreeDropDown = factory();
  }
}(this, function () {
	/***
	 * <div style="position:relative">
	 * 	<input type="text"></input>
	 * </div>
	 */
	var TreeDropDown=function(config){
		this.treeId='treeDropDown'+TreeDropDown.count++;
		//说明文字
		this.selectType='radio';	//radio|checkbox
		this.checkOnlyChild=true;
		this.$input={};		//
		this.$inputValue={};
		this.desc='请选择';	//空值描述的文字
		this.idField='id';
		this.simpleData=true;
		this.parentIdField='pId';
		this.isParent=function(record){return record.isParent===true},
		this.nameField='name';	//tree节点名称字段
		this.valueField='id';	//作为值的数据字段
		this.name='';		//选中的名称
		this.value='';		//选中的值
		this.dataUrl='';	//tree的数据url
		this.treeData=[];	//tree的数据
		this.expandAll=true;
		this.readOnly=false;
		this.onAfterSelectNode=function(name,value,treeNodes){};
		$.extend(true,this,config);
		this.$parent=this.$input.parent();
		this.init();
	};
	TreeDropDown.count=0;
	TreeDropDown.prototype={
		render:function(){
			if(!this.readOnly)
			{
				//初始化input
				this.$input.css({
					"background-image": "url('/resources/images/triangle-botton-16.png')",
					"background-repeat": "no-repeat",
					"background-position": "right",
					"background-color": "#fff",
					cursor: "pointer"
				});
			}	
			this.$input.prop('readonly',true);
			this.$input.attr("placeholder",this.desc);
			
			
			//添加下拉框div
			this.$parent.append('<div class="dropDiv" style="position: absolute;display:none"><ul id="'+this.treeId+'" class="ztree dropTree"></ul></div>');
			//树的容器
			this.$treeDiv=$('.dropDiv',this.$parent);
			this.$tree=$('#'+this.treeId,this.$parent);
			this.$treeDiv.css({
				width: this.$input.outerWidth(true)-2,
				padding:'5px 0',
				"z-index": 100,
				"background-color": '#fff',
				border: '1px solid #cccccc',
				"border-radius": '3px'
			});
			//初始化树
			this.initTree();
		},
		initTree:function(){
			var that=this,treeSetting = {
					data : {
						simpleData : {
							enable : this.simpleData
						}
					},
					check:{
						chkStyle:this.selectType,
						chkboxType: { "Y": "", "N": "" },
						radioType:"all",
						enable: true
					},
					view : {
						selectedMulti : false
					},
					callback: {
						//扩张的ztree事件：appendNodes的data.initNode之后
						afterInitNode:function(treeNode){
							if(that.checkOnlyChild)
								treeNode.nocheck=treeNode.isParent;
						},
						onCheck:function(event, treeId, treeNode){
							that.afterSelectNode(that.ztreeObj.getCheckedNodes(true));
							if(treeNode.checked)
							{
								that.ztreeObj.selectNode(treeNode);
								that.selectType=='radio'&&that.showOrHide();
							}
							return true;
						},
						onClick: function(event, treeId, treeNode){
							if(treeNode.nocheck)
								return;
							if(!treeNode.checked)
							{
								that.ztreeObj.checkNode(treeNode,true);
								(that.selectType=='radio')&&that.showOrHide();
							}	
							else
								that.ztreeObj.checkNode(treeNode,false);
							that.afterSelectNode(that.ztreeObj.getCheckedNodes(true));
							return false;
						}
					}
				};
				if(this.simpleData)
					treeData=$.map(this.getTreeData(),function(ele){
						return {
							id:ele[that.idField],
							name:ele[that.nameField]||'',
							pId:ele[that.parentIdField]||0,
							isParent:that.isParent(ele),
							nocheck:that.isParent(ele),
							_value:ele[that.valueField],
							data:ele
						};
					});
				else
					treeData=$.map(this.getTreeData(),this.translateData.bind(this));
			this.ztreeObj = $.fn.zTree.init(this.$tree, treeSetting, treeData);
			if(this.expandAll)
				this.ztreeObj.expandAll(true);
		},
		translateData:function(data){
			var res={
				id:data[this.idField],
				name:data[this.nameField]||'',
		 		pId:data[this.parentIdField]||0,
		 		isParent:this.isParent(data),
		 		nocheck:this.isParent(data),
		 		_value:data[this.valueField],
		 		data:data
			};
			if(data.children&&data.children.length>0)
				res.children=$.map(data.children,this.translateData.bind(this));
			return res;
		},
		afterSelectNode:function(treeNodes){
			var nameField=this.nameField,valueField=this.valueField;
			if(treeNodes.length==0)
			{
				this.name='';		//选中的名称
				this.value='';		//选中的值
			}	
			else if(treeNodes.length==1)
			{
				this.name=treeNodes[0].data[nameField];		//选中的名称
				this.value=treeNodes[0].data[valueField];		//选中的值
			}
			else 
			{
				this.name=$.map(treeNodes,function(n){return n.data[nameField];}).join(',');
				this.value=$.map(treeNodes,function(n){return n.data[valueField];}).join(',');
			}
			this.$input.val(this.name);
			this.$inputValue.val(this.value);
			this.onAfterSelectNode&&this.onAfterSelectNode(this.name,this.value,treeNodes);
		},
		setValue:function(value){
			if(value==null)
				return;
			if(value.length==0)
				return;
			value+='';
			var that=this,
				treeNodes=$.map(value.split(','),function(v){
					var treeNode=that.ztreeObj.getNodeByParam("_value",v);
					if(treeNode==null)
						return;
					that.ztreeObj.checkNode(treeNode, true, false);
					return treeNode;
				});
			this.afterSelectNode(treeNodes);
		},
		getTreeData:function(){
			var res=this.treeData;
			if(this.dataUrl)
				$.ajax(this.dataUrl,{
					type:'POST',
					dataType:'json',
					async:false
				}).done(function(data){
					res=data;
				}).fail(function(){
				});
			return res;
		},
		bind:function(){
			var that=this;
			if(!this.readOnly)
			{
				this.$input.on('click',function(){
					that.showOrHide();
				});
			}	
		},
		showOrHide:function(){
			if(this.$treeDiv.css('display')=='block')
				this.$treeDiv.css('display','none');
			else
			{
				this.$treeDiv.css({
					width: this.$input.outerWidth(true)-2
				});
				this.$treeDiv.css('display','block');
			}
		},
		fillData:function(){
			this.value!=null&&this.setValue(this.value);
		},
		init:function(){
			this.render();
			this.bind();
			this.fillData();
		}
	};
	
	return TreeDropDown;
}));